
    
     function UpdateMonthOutgo(currDate)
     {
        var newOutgo = datamodel.getMonthOutgo(currDate.getFullYear(),(currDate.getMonth() + 1));
        var dateStr = currDate.getFullYear().toString() + (currDate.getMonth() + 1).toString();
         $("#outgo" + dateStr).html(newOutgo);
     }
     function UpdateMonthIncome(currDate)
     {
        var newIncome = datamodel.getMonthIncome(currDate.getFullYear(),(currDate.getMonth() + 1));
        var dateStr = currDate.getFullYear().toString() + (currDate.getMonth() + 1).toString();
        $("#income" + dateStr).html(newIncome);
     }
     
    function SaveData(index)
    {
        var oldTurnover = 
                    datamodel.turnovers[index];
        if(oldTurnover)
        {
            oldTurnover.money = $("#turnovereditmoney" + oldTurnover.id).val();
            $("#turnovershowmoney" + oldTurnover.id).html(oldTurnover.money);
            oldTurnover.remark = $("#turnovereditremark" + oldTurnover.id).val();
            $("#turnovershowremark" + oldTurnover.id).html(oldTurnover.remark);
            
            oldTurnover.time = $("#turnovereditdate" + oldTurnover.id).val();
            var currDate = StrToDate(oldTurnover.time);
            var timeStr = currDate.format("yyyy-MM-dd");
             $("#turnovershowdate" + oldTurnover.id).html(timeStr);
            

            //不是转账的话
            if(oldTurnover.type != 2)
            {
                oldTurnover.catid = $("#turnovereditcatid" + oldTurnover.id).val();
            
               var liclsname = catidToClassStr(oldTurnover.catid) + " li";
               var iconclsname = catidToClassStr(oldTurnover.catid) + "-icon";
                $("#turnovercaticon" + oldTurnover.id).attr("class",iconclsname);
                $("#turnovercatli" + oldTurnover.id).attr("class",liclsname);
                
                var cat = datamodel.getCategory(oldTurnover.catid);
                $("#turnovershowcat" + oldTurnover.id).html(cat.name);
                oldTurnover.catid
                //收入可以更改to
                if(oldTurnover.type == 1)
                {
                     oldTurnover.to = $("#turnovereditaccount" + oldTurnover.id).val();
                     UpdateMonthIncome(currDate)
                     
                    
                } else if(oldTurnover.type == 0)//支出改from
                {
                     oldTurnover.from = $("#turnovereditaccount" + oldTurnover.id).val();
                     UpdateMonthOutgo(currDate  );
                }
            }
            
            oldTurnover.Update();
        }
    }
    
    function InitTurnover(){
    	$("#turnoverList").html("");
        datamodel.init();
        
        var dates = new Array();
        var incomes = new Array();
        var outgos = new Array();
        
        var index = 0;
        for(var key in datamodel.turnovers)
        {
            var itemData = datamodel.turnovers[key];
            var currDate = StrToDate(datamodel.turnovers[key].time);
            //month从0开始
            var dateStr = currDate.getFullYear().toString() + (currDate.getMonth() + 1).toString();
            var listviewIDStr = "listview" + dateStr;//用于标识某个具体月份的列表,用于后续添加数据用,比如12年3月的是listview20123
            
            if(!arrayexist(dates,dateStr))
            {
                dates.push(dateStr);
                var collapsed = (index != 0);
                var hideStr = "style=\"display:none;\"";
                var monthIcon;
                if(!collapsed)
                {
                    hideStr = "";
                    monthIcon = "icon-minus";
                } else 
                {
                    monthIcon = "icon-plus";
                }

                 var monthHTML = 
                "<div class=\"month\"><a href=\"#\"class=\"openMonth\"><ul class=\"bg\"><li>" + 
                "<i data-trigger=\"trigger\"  class=\"{4}\"></i>" + 
                "</li><li>{0}月</li><li>收：￥<span id=income{1}>0</span></li><li>支：￥<span id=outgo{1}>0</span>"+
                "</li> </ul></a><div class=\"category\" {3} ><ul  id={2}></ul></div>  </div>"
                  monthHTML = 
                  monthHTML.format((currDate.getMonth() + 1),dateStr,listviewIDStr,hideStr,monthIcon);
                 
                  $('#turnoverList').append(monthHTML);
            }
            //计算各月份的收入和支出
            switch(itemData.type)
            {
                case "0": 
                 {
                    var now = parseInt($('#outgo' + dateStr).html());
                    now += parseInt(itemData.money);
                    $('#outgo' + dateStr).html(now);
                 }
                 break;
                case "1":
                 {
                    var now = parseInt($('#income' + dateStr).html());
                    now += parseInt(itemData.money);
                    $('#income' + dateStr).html(now);
                 }
                 break;
                case "2":
                   break;
            }
            //计算各月份的收入和支出 结束
            
/*

                                        
 */            
             ///////////////////////////////////////////////////////////////////////
             //生成账户选择框
             var accountSelectHTML = "";
             if(itemData.type != 2)
             {
                  var accountsSelect = "";
             for(var accountid in datamodel.accounts)
             {
                var selectString = "";
                var account = datamodel.accounts[accountid];
                if(itemData.type == 1)
                {
                    if(account.id == itemData.to)
                    {
                        selectString = "selected=\"selected\"";
                    }
                } else (itemData.type == 0)
                {
                    if(account.id == itemData.from)
                    {
                        selectString = "selected=\"selected\"";
                    }
                }
                
                 
                 
                 var oneSelect = "<option value='{0}' {2}>{1}</option>"
                 oneSelect = oneSelect.format(account.id,account.name,selectString);
                 accountsSelect += oneSelect;
             }
             
             accountSelectHTML = 
             "<li><label>账户:</label>"
             + "<select name=\"account\" class=\"select\" id='turnovereditaccount{0}'>"
             + accountsSelect
             + "</select></li>";
             accountSelectHTML = accountSelectHTML.format( datamodel.turnovers[key].id);
             }
            ///////////////////////////////////////////////////////////////////////
             //生成类别选择框
             var categorySelectHTML = "";
             if(itemData.type != 2)
             {
                  var categorysSelect = "";
             for(var categoryid in datamodel.categorys)
             {
                var selectString = "";
                var category = datamodel.categorys[categoryid];
                if(category.id == itemData.catid)
                {
                     selectString = "selected=\"selected\"";
                }
                
                 
                 if(category.type == itemData.type)
                 {
                     var oneSelect = "<option value='{0}' {2}>{1}</option>"
                     oneSelect = oneSelect.format(category.id,category.name,selectString);
                     categorysSelect += oneSelect;
                 }
                 
             }
                       
             categorySelectHTML = 
             "<li><label>类别:</label> "
             + "<select name=\"category\" class=\"select\" id='turnovereditcatid{0}'>"
             + categorysSelect
             + "</select></li>";
             categorySelectHTML = categorySelectHTML.format( datamodel.turnovers[key].id);
             
             }
            
              
              ///////////////////////////////////////////////////////////////////////
             
             

           
             ///////////////////////////////////////////////////////////////////////
            //初始化整个item的html
              
              var  itemHTML = "<li class=\"{6} li\" id='turnovercatli{0}'><a href=\"\"  class=\"openCategory\">" + 
                              "<i class=\"{6}-icon\" id='turnovercaticon{0}'></i><div class=\"left\">" +
                              "<span class=\"accoutname\" id='turnovershowcat{0}'>{4}</span><br/><span class=\"date\" id='turnovershowdate{0}'>{3}</span>" +  
                              "<span class=\"remark\" id='turnovershowremark{0}'>{2}</span></div><div class=\"right\">"+
                              "<span class=\"{5}\">￥<span id ='turnovershowmoney{0}'>{1}</span></span><span data-trigger=\"trigger\" class=\"arrow\"></span>"+
                              "</div></a>" + 
                              //编辑框
                              "<div class=\"fold\" id = 'turnoveredit{0}' data-turnoverindex='{7}'><form><ul class=\"form-list sub-list\"><li><label>金额:</label> "+
                              "<input type=\"text\" name=\"money\" class=\"ipt\" value=\"{1}\" id='turnovereditmoney{0}'/></li>" +   
                              accountSelectHTML + 
                              categorySelectHTML +
                              "<li><label>时间:</label>"  + 
                              "<input type=\"date\" class=\"ipt\"  name=\"date\" value=\"{3}\" id='turnovereditdate{0}'/></li>" + 
                              "<li><label>备注:</label><textarea class=\"ipt\" style=\"height: 100px;\" id='turnovereditremark{0}'>{2}</textarea>"+
                              "</li><li><button type=\"button\" class=\"button cancel\" onclick=\"SaveData({7})\">保存</button>&nbsp&nbsp&nbsp&nbsp<button type=\"button\" class=\"button cancel\">取消</button>"+        
                              "</li></ul></form></div>"            
                                    
                   
              
  
              var timeStr = currDate.format("yyyy-MM-dd");
              var cat = datamodel.getCategory(itemData.catid);
              var leftMainStr = ""
              if(cat)
              {
                 leftMainStr = cat.name;
              } 
              if(itemData.type == 2)
              {
                
                leftMainStr = "{0} -> {1}";
                var accountFrom = datamodel.getAccount(itemData.from);
                var accountTo = datamodel.getAccount(itemData.to);
                leftMainStr = leftMainStr.format(accountFrom.name,accountTo.name);
              }
              
              var moneyColorClass = "money";
              if(itemData.type == 1)
              {
                moneyColorClass = "money red";
              }
              
              var catClassName = catidToClassStr(itemData.catid); 
              if(itemData.type == 2)
              {
                catClassName = "zhuanzhang"; 
              }
             
              itemHTML = 
              itemHTML.format(itemData.id,itemData.money,itemData.remark,
              timeStr,leftMainStr,moneyColorClass,catClassName,key);
              
              
              $('#' + listviewIDStr).append(itemHTML);
            //初始化整个item的html 结束
            ++ index;
             
        }
    }